package com.kude.service;

import java.util.List;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kude.entity.ProductCategory;
import com.kude.manager.ProductCategoryManager;
import com.kude.manager.OperException;
import com.kude.util.BeanFactory;

public class ProductCategoryService {
	private static final Logger logger = LoggerFactory
			.getLogger(ProductCategoryService.class.getName());

	private ProductCategoryManager productCategoryManager;

	/**
	 * 获取指定ID的类别
	 * 
	 * @param id
	 * @return
	 * @throws OperException
	 */
	public ProductCategory getProductCategory(int id) throws OperException {
		return productCategoryManager.get(id);
	}

	/**
	 * 增加类别
	 * 
	 * @param productCategory
	 * @return
	 * @throws OperException
	 */
	public boolean addProductCategory(ProductCategory productCategory)
			throws OperException {
		// TODO 保证Prefix的唯一
		productCategoryManager.add(productCategory);
		return true;

	}

	/**
	 * 更新产品类别
	 * 
	 * @param productCategory
	 * @throws OperException
	 */
	public void updateProductCategory(ProductCategory productCategory)
			throws OperException {
		productCategoryManager.update(productCategory);
	}

	/**
	 * 删除产品类别
	 * 
	 * @param id
	 * @throws OperException
	 */
	public void deleteProductCategory(int id) throws OperException {
		productCategoryManager.delete(id);
	}

	/**
	 * 根据类别前缀获取产品的类别ID集合
	 * 
	 * @param articleManager
	 * @throws OperException
	 */
	public List<ProductCategory> getProductCategoryListByPrefix(String prefix)
			throws OperException {
		return productCategoryManager.find(Restrictions.like("prefix", prefix + "%",MatchMode.ANYWHERE));
	}

	/**
	 * 通过parentID来获取属于同一大类别的小类别
	 * 
	 * @param parentId
	 * @return
	 * @throws OperException
	 */
	public List<ProductCategory> getProductCategoryListByParentId(int parentId)
			throws OperException {
		return productCategoryManager.find(Restrictions.eq("parentId", parentId));
	}
    /**
     * 获得所有类别
     * @return
     */
	public List findAll() {
		return productCategoryManager.findAll();
	}

	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}


}
